package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.OverflowListDao;
import com.atguigu.jxc.dao.OverflowListGoodsDao;
import com.atguigu.jxc.entity.DamageList;
import com.atguigu.jxc.entity.DamageListGoods;
import com.atguigu.jxc.entity.OverflowList;
import com.atguigu.jxc.entity.OverflowListGoods;
import com.atguigu.jxc.service.OverflowListGoodsService;
import com.atguigu.jxc.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class OverflowListGoodsServiceImpl implements OverflowListGoodsService {
    @Autowired
    private OverflowListGoodsDao overflowListGoodsDao;
    @Autowired
    private OverflowListDao overflowListDao;

    @Override
    public void save(OverflowList overflowList, List<OverflowListGoods> damageListGoods) {
        //获取主键
        overflowListDao.save(overflowList);
        //
        damageListGoods.forEach(a -> {
            a.setOverflowListId(overflowList.getOverflowListId());
        });
        damageListGoods.forEach(a -> {
            overflowListGoodsDao.save(a);
        });


    }

    @Override
    public Map<String, Object> list(String sTime, String eTime) {
        Map<String, Object> result = new HashMap<>();
        List<OverflowList> list = overflowListDao.list();
        List<OverflowList> collect = list.stream().filter(a -> {
            try {
                return DateUtil.getTimeSlotByDay(sTime, eTime).contains(a.getOverflowDate());
            } catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        }).collect(Collectors.toList());
        result.put("rows", collect);
        return result;
    }

    @Override
    public Map<String, Object> goodsList(Integer overflowListId) {
        Map<String, Object> result = new HashMap<>();
        List<OverflowListGoods> list = overflowListGoodsDao.listAlarm(overflowListId);
        result.put("rows", list);
        // result.put("total", list.size());
        return result;
    }
}
